package com.scy.boot04test02.controller;

import com.scy.boot04test02.entity.Books;
import com.scy.boot04test02.mapper.BooksMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BooksController {

    @Autowired
    BooksMapper booksMapper;

    @RequestMapping("/insertBook")
    public String insert(Books books){
        booksMapper.insert(books);
        return "添加成功<a href='/'>返回首页</a>";
    }
    @RequestMapping("/selectBook")
    public String select(){
        List<Books> list = booksMapper.select();
        //把list集合中的数据装金表格标签
        String html = "<table border='1'>";
        html+="<caption>书籍列表</caption>";
        html+="<tr><th>id</th><th>书名</th><th>价格</th><th>页数</th><th>作者</th><th>操作</th></tr>";
        for (Books b :
                list) {
            html+="<tr>";
            html+="<td>"+b.getId()+"</td>";
            html+="<td>"+b.getName()+"</td>";
            html+="<td>"+b.getPrice()+"</td>";
            html+="<td>"+b.getPage()+"</td>";
            html+="<td>"+b.getAuthor()+"</td>";
            //http://localhost:8080/delete?id=xxx
            html+="<td><a href='/deleteBook?id="+b.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html+"<br/>"+"<a href='/'>返回首页</a>";

    }
    @RequestMapping("/deleteBook")
    public String delete(int id){
        booksMapper.delete(id);
        return "删除成功<a href='/'>返回首页</a>";
    } @RequestMapping("/updateBook")
    public String update(Books books){
        booksMapper.update(books);
        return "更新成功<a href='/'>返回首页</a>";
    }
}
